package com.sdgakj.web.manager;

import com.google.common.collect.Maps;
import com.sdgakj.entity.Permission;
import com.sdgakj.entity.Role;
import com.sdgakj.entity.RolePermission;
import com.sdgakj.enums.EnumIsDelete;
import com.sdgakj.service.manager.PermissionService;
import com.sdgakj.service.manager.RolePermissionService;
import com.sdgakj.service.manager.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value="/rolepermission")
public class RolePermissionController {
	
	@Autowired
	private RolePermissionService rolePermissionService;
	
	@Autowired
	private PermissionService permissionService;
	
	@Autowired
	private RoleService roleService;
	
	private static Logger logger = LoggerFactory.getLogger(RolePermissionController.class);

	
	
	private static Map<String,String> sortTypes =Maps.newLinkedHashMap();
	
	static {
		sortTypes.put("auto", "自动");
	}

	/**
	 * 跳转授权页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/accredit",method=RequestMethod.GET)
	public String accredit(@RequestParam(value="roleid",defaultValue="0")Long roleid,Model model){
		List<Long> checkList =new  ArrayList<Long>();
		Role role =roleService.getRoleById(roleid);
		// 获取已经授权的结果
		List<RolePermission> checkedList =rolePermissionService.getCheckedPermission(roleid);
		for(RolePermission rp :checkedList){
			checkList.add(rp.getPermission().getId());
		}
		// 获取总权限的结果
		String result =permissionService.getAllPermission(EnumIsDelete.NOTDELETE.getCode(),checkList);
		System.out.println(result);
		System.out.println(checkedList.size());
		model.addAttribute("result", result);
		model.addAttribute("checkedlist", checkList);
		model.addAttribute("role", role);
		model.addAttribute("action","create");
		return "manager/accredit";
	}
	
	/**
	 * 添加权限
	 * @param Source
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public String createRolePermission(@RequestParam("roleid")Long roleid,@RequestParam(value="allpermission",defaultValue="")String arrayresult,RolePermission entity,RedirectAttributes redirectAttributes){
		Role role=roleService.getRoleById(roleid);
//		List<RolePermission>  rpList =new ArrayList<RolePermission>();
		List<RolePermission>  rpList =role.getPermissionlist();
		if(!CollectionUtils.isEmpty(rpList)){
			rpList.clear(); //清空所有的权限值			
		}
		//当前选中的值
		if(StringUtils.isNotEmpty(arrayresult)){
			String[] checkarray =arrayresult.split(","); 
			for(int i=0;i<checkarray.length;i++){
				RolePermission rp =new RolePermission(role,new Permission(Long.valueOf(checkarray[i])));
				rpList.add(rp);
			}
		}
		role.setPermissionlist(rpList);
		roleService.saveRole(role); 
		redirectAttributes.addFlashAttribute("message", "添加成功！");
		return "redirect:/role/list";
	}
	
	
	
	/**
	 * 删除授权信息
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="/delete/{id}",method=RequestMethod.GET)
	public String delete(@PathVariable("id") Long id,RedirectAttributes redirectAttributes){
		RolePermission rolePermission =rolePermissionService.getRolePermissionById(id);
		rolePermissionService.saveRolePermission(rolePermission);
		redirectAttributes.addFlashAttribute("message", "删除成功！");
		return "redirect:/permission/list";
	}
		

	
	
	@ModelAttribute
	public void getPermission(@RequestParam(value ="id",defaultValue = "-1")Long id ,Model model){
		if(id != -1){
			model.addAttribute("rolepermission", rolePermissionService.getRolePermissionById(id));
		}
	}
}
